//
//  NSAttributedString+TGAddition.m
//  Tools
//
//  Created by 罗先森 on 2017/8/8.
//  Copyright © 2017年 Trouble Group. All rights reserved.
//

#import "NSAttributedString+TGAddition.h"

@implementation NSAttributedString (TGAddition)


+ (instancetype)tg_imageTextWithImage:(UIImage *)image imageWH:(CGFloat)imageWH title:(NSString *)title fontSize:(CGFloat)fontSize titleColor:(UIColor *)titleColor spacing:(CGFloat)spacing {
    
    // 文本字典
    NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
                                NSForegroundColorAttributeName: titleColor};
    NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:spacing]};
    
    // 图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
    attachment.bounds = CGRectMake(0, 0, imageWH, imageWH);
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    // 换行文本
    NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"\n\n" attributes:spacingDict];
    
    // 按钮文字
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];
    
    // 合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:imageText];
    [attM appendAttributedString:lineText];
    [attM appendAttributedString:text];
    
    return attM.copy;
}

+ (instancetype)tg_imageTextWithImage:(UIImage *)image
                              imageWH:(CGFloat)imageWH
                            leftTitle:(NSString *)title
                             fontSize:(CGFloat)fontSize
                           titleColor:(UIColor *)titleColor
                              spacing:(CGFloat)spacing {
    
    // 文本字典
    NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
                                NSForegroundColorAttributeName: titleColor};
    NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:spacing]};
    
    // 图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
    
   
    attachment.bounds = CGRectMake(0, 2, imageWH, imageWH);
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    // 换行文本
    NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"  " attributes:spacingDict];
    
    // 按钮文字
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];
    
    // 合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:text];
    [attM appendAttributedString:lineText];
    [attM appendAttributedString:imageText];
    
    return attM.copy;
}


+ (instancetype)tg_imageTextWithImage:(UIImage *)image
                               imageW:(CGFloat)imageW imageH:(CGFloat)imageH
                            leftTitle:(NSString *)title
                             fontSize:(CGFloat)fontSize
                           titleColor:(UIColor *)titleColor
                              spacing:(CGFloat)spacing {
    
    // 文本字典
    NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
                                NSForegroundColorAttributeName: titleColor};
    NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:spacing]};
    
    // 图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
   
    attachment.bounds = CGRectMake(0, 0, imageW, imageH);
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    // 换行文本
    NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"\n\n" attributes:spacingDict];
    
    // 按钮文字
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];
    
    // 合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:text];
    [attM appendAttributedString:lineText];
    [attM appendAttributedString:imageText];
    
    return attM.copy;
}


+ (instancetype)tg_imageTextWithTitle:(NSString *)title{
    
    //截取字符串
    
    NSRange range1 = [title rangeOfString:@"【"];
    NSString *result1 = [title substringToIndex:range1.location];
    
    
    NSRange startRange2 = [title rangeOfString:@"【"];
    NSRange endRange2 = [title rangeOfString:@"】"];
    NSRange range2 = NSMakeRange(startRange2.location + startRange2.length, endRange2.location - startRange2.location - startRange2.length);
    NSString *result2 = [title substringWithRange:range2];
    
    
    //文本
    
    
    
    
    // 文本字典
    NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:13],
                                NSForegroundColorAttributeName: AdColor};
    NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
    
    // 文本字典
    NSDictionary *titleDict2 = @{NSFontAttributeName: [UIFont systemFontOfSize:13],
                                 NSForegroundColorAttributeName: AdColor};
    
    
    
    
    
    // 图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"adCopyImage"];
    attachment.bounds = CGRectMake(0, -6, 50, 20);
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    // 换行文本
    NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"    " attributes:spacingDict];
    // 换行文本
    NSAttributedString *lineText1 = [[NSAttributedString alloc] initWithString:@"  " attributes:spacingDict];
    
    // 按钮文字
    NSAttributedString *text1 = [[NSAttributedString alloc] initWithString:result1 attributes:titleDict];
    
    NSAttributedString *text2 = [[NSAttributedString alloc] initWithString:result2 attributes:titleDict2];
    
    
    
    // 合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:text1];
    [attM appendAttributedString:lineText1];
    [attM appendAttributedString:text2];
    [attM appendAttributedString:lineText];
    
    if ([title containsString:@"（"]) {
        [attM appendAttributedString:imageText];
    }else{
        
        NSArray *array = [title componentsSeparatedByString:@"】"];
        
          NSAttributedString *text3 = [[NSAttributedString alloc] initWithString:array.lastObject attributes:titleDict];
        
        [attM appendAttributedString:text3];
        
    }
    
    return attM.copy;
    
}





/// 使用图像和文本生成 左文字右图像 的属性文本
///
/// @param image      图像
/// @param imageWH    图像宽高
/// @param title      标题文字
/// @param fontSize   标题字体大小
/// @param titleColor 标题颜色
/// @param spacing    图像和标题间距
///
/// @return 属性文本
+ (instancetype)tg_RightImageTextWithImage:(UIImage *)image
                                    imageW:(CGFloat)imageW imageH:(CGFloat)imageH
                                 leftTitle:(NSString *)title
                                  fontSize:(CGFloat)fontSize
                                titleColor:(UIColor *)titleColor
                                   spacing:(CGFloat)spacing{
    
    
    
    // 文本字典
    NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
                                NSForegroundColorAttributeName: titleColor};
    NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:spacing]};
    
    // 图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
    
    
    attachment.bounds = CGRectMake(0, 2, imageW, imageH);
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    // 换行文本
    NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"  " attributes:spacingDict];
    
    // 按钮文字
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];
    
    // 合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:text];
    [attM appendAttributedString:lineText];
    [attM appendAttributedString:imageText];
    
    return attM.copy;
    
    
}

@end


